package com.example.backend.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.backend.common.EncryptUtil;
import com.example.backend.common.Result;
import com.example.backend.entity.User;
import com.example.backend.entity.UserCard;
import com.example.backend.mapper.UserMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import static java.lang.Integer.parseInt;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
public class UserController {
    @Resource
    UserMapper userMapper;
    @RequestMapping(value ="/user",method = GET)
    String getUserData(@RequestParam(value = "openId", required = true) String openId ){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("open_id",openId);
        User user=userMapper.selectOne(wrapper);
        System.out.println(user);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        String result=null;
        try {
            result= EncryptUtil.encrypt(Convert.toStr(mapper.writeValueAsString(user)));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    @PutMapping(value = "/user/update")
    void updateUserData(@RequestParam(value="data",required=true) String data){
        System.out.println(data);
        String decrypt=null;
        try {
            decrypt= EncryptUtil.decrypt(data);
        } catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(decrypt);
        JSONObject jsonObject = JSONUtil.parseObj(decrypt);
        System.out.println(jsonObject.getStr("nickName"));
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("open_id",jsonObject.getStr("openId"));
        User user=new User();
        user.setNickName(jsonObject.getStr("nickName"));
        user.setSex(parseInt(jsonObject.getStr("sex")));
        user.setDate(DateUtil.parse(jsonObject.getStr("date")));
        String src=jsonObject.getStr("image");
        String str[]=src.split("/");
        src=str[str.length-1];
        user.setImage(src);
        user.setAddress(jsonObject.getStr("address"));
        userMapper.update(user,updateWrapper);
    };
    @RequestMapping(value = "/user/insert",method=GET)
    String insertUserData(String openId){
        User user=new User();
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("open_id",openId);
        user=userMapper.selectOne(wrapper);
        System.out.println("--------------------------------");
        System.out.println(user);
        System.out.println("--------------------------------");
        if(user==null){
            user=new User();
            user.setOpenId(openId);
            user.setSex(0);
            user.setNickName("无");
            user.setImage("man.png");
            userMapper.insert(user);
        }
        return null;
    }
}
